package com.uestc.admin;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

import com.google.protobuf.ServiceException;
import com.uestc.coprocessor.registerLocation.ProvinceRegisterQuery;
import com.uestc.coprocessor.userLog.UserLogAggregationClient;
import com.uestc.hbase.MTable;
import com.uestc.util.JsonUtil;
import com.uestc.util.TimeUtil8;

/**
 * 向前端返回关于用户管理的信息
 * 
 * @author justyoung
 *
 */
public class UserAdmin implements AdminBase {

	private String columnName = "UserColumns";
	private String tableName = "user";

	@Override
	public String getInfo(int page, int limit, HttpServletRequest request) throws Throwable {
		return this.getDetailInfo(tableName, "UserColumns", Arrays.asList(new String[] { "info" }), page, limit,
				request);
	}

	@Override
	public String getColumnName() {
		return columnName;
	}

	@Override
	public String getTableName() {
		return tableName;
	}

	@Override
	public String getInfoWithCondition(int page, int limit, HashMap<String, String> conditions,
			HttpServletRequest request) throws Throwable {
		return this.getDetailInfo(tableName, "UserColumns", Arrays.asList(new String[] { "info" }), page, limit,
				request, "info", conditions);
	}

	/**
	 * 获取注册用户在全国的分布信息，返回Json字符串
	 * 
	 * @return
	 * @throws Throwable
	 * @throws ServiceException
	 */
	public String getRegisterUserDistribution() throws ServiceException, Throwable {
		ProvinceRegisterQuery query = new ProvinceRegisterQuery();
		MTable mTable = new MTable(tableName);
		try {
			return query.getResult(mTable.getTable());
		} finally {
			mTable.close();
		}
	}

	/**
	 * 返回全国各省用户频繁操作次数的地图
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return
	 * @throws ServiceException
	 * @throws Throwable
	 */
	public String getActiveUserDistribution(String beginDate, String endDate) throws ServiceException, Throwable {
		UserLogAggregationClient client = new UserLogAggregationClient();
		MTable mTable = new MTable("userlog");
		try {
			return client.getActiveUserMap(mTable.getTable(), beginDate, endDate);
		} finally {
			mTable.close();
		}
	}

	/**
	 * 根据日期查询每天新增的用户
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return
	 * @throws Throwable
	 */
	public String getNewRegistrationNumber(String beginDate, String endDate) throws Throwable {
		List<String> days = TimeUtil8.getDatesBetweenTowDates(beginDate, endDate);
		MTable hisTable = new MTable("historylog");
		MTable userTable = new MTable("user");
		String hRowKey = null;
		Map<String, String> item = new LinkedHashMap<>();
		try {
			for (String day : days) {
				hRowKey = "register:" + day;
				if (hisTable.isRowExists(hRowKey)) {
					String l = hisTable.getValue(hRowKey, "info", "register-num");
					item.put(day, l);
				} else {
					Long l = userTable.getRowCounterWithCondition("user", "info", "create_time", day,
							MTable.FILTER_COMBINE.INCLUDE);
					String today = TimeUtil8.getToday();
					if (!today.equals(day))
						hisTable.setData(hRowKey, "info", "register-num", l.toString());
					item.put(day, l.toString());
				}
			}
		} finally {
			hisTable.close();
			userTable.close();
		}
		return JsonUtil.createJsonString(item);
	}

	/**
	 * 将用户流量恢复为0
	 * 
	 * @throws IOException
	 */
	public void recoverUsedFlow() throws IOException {
		MTable userTable = new MTable("user");
		try {
			Scan scan = new Scan();
			scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("used_flow"));
			ResultScanner rs = userTable.getTable().getScanner(scan);
			for (Result r : rs) {
				byte[] row = r.getRow();
				Put put = new Put(row);
				put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("used_flow"), Bytes.toBytes("0"));
				userTable.getTable().put(put);
			}
		} finally {
			userTable.close();
		}
	}

	@Override
	public String getDateColumnName() {
		return "create_time";
	}

}
